package de.le.mfw.client.main.presenter;

import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.HasClickHandlers;

import de.le.mfw.client.main.MainEventBus;
import de.le.mfw.client.main.service.UserServiceAsync;
import de.le.mfw.client.main.view.UserViewImpl;
import de.le.mfw.client.util.LoadingScreenCallback;
import de.le.mfw.client.widget.InfoPopup;
import de.le.mfw.shared.UserDTO;

@Presenter(view = UserViewImpl.class)
public class UserPresenter extends BasePresenter<UserViewImpl, MainEventBus> {

	public interface UserView extends IsWidget {
		public HasClickHandlers getLoginButton();

		public HasClickHandlers getRegisterNewUserButton();

		public HasClickHandlers getForgotPasswordButton();

		public HasClickHandlers getLogoutButton();

		public boolean isValid();

		public String getNick();

		public String getPassword();

		public String getEmail();

		public void onRegistered();

		public void onLoggedIn(String nick);

		public void onLoggedOut();

		public void onSendPassword();
	}

	@Inject
	private UserServiceAsync userService;

	@Override
	public void bind() {
		view.getLoginButton().addClickHandler(new LoginButtonClickHandler());

		view.getRegisterNewUserButton().addClickHandler(
				new RegisterNewUserClickHandler());

		view.getForgotPasswordButton().addClickHandler(
				new ForgotPasswordClickHandler());

		view.getLogoutButton().addClickHandler(new LogoutClickHandler());
	}

	private final class ForgotPasswordClickHandler implements ClickHandler {
		public void onClick(final ClickEvent event) {
			if (view.isValid()) {
				String nick = view.getNick();
				userService.sendNewPassword(nick,
						new LoadingScreenCallback<Void>(event) {

							@Override
							protected void success(Void result) {
								view.onSendPassword();
								new InfoPopup(
										"Neues Passwort an deine Email-Adresse versendet")
										.show();
							}
						});
			}
		}
	}

	private final class RegisterNewUserClickHandler implements ClickHandler {
		public void onClick(final ClickEvent event) {
			if (view.isValid()) {
				String nick = view.getNick();
				String password = view.getPassword();
				final String email = view.getEmail();
				userService.register(nick, email, password,
						new LoadingScreenCallback<Void>(event) {

							@Override
							protected void success(Void result) {
								view.onRegistered();
								new InfoPopup(
										"Bestätigung der Registrierung an die Email-Adresse: "
												+ email + " versendet").show();
							}
						});
			}
		}
	}

	private final class LoginButtonClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			if (view.isValid()) {
				final String nick = view.getNick();
				String password = view.getPassword();
				userService.login(nick, password,
						new LoadingScreenCallback<UserDTO>(event) {

							@Override
							protected void success(UserDTO user) {
								view.onLoggedIn(nick);
								eventBus.login(user);
							}

						});
			}
		}
	}

	private final class LogoutClickHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			view.onLoggedOut();
			eventBus.logout();
		}
	}

	public void onStart() {
		eventBus.setUserWidget(view.asWidget());
	}
}